const { regClass, property } = Laya;
@regClass()
export default class PlayerController extends Laya.Script {
    @property(Number)
    // 移动速度（米/秒）
    public moveSpeed: number = 5;
    @property(Number)
    // 旋转速度（度/秒）
    public rotateSpeed: number = 180;

    private _rigidbody: Laya.Rigidbody3D;
    private _transform: Laya.Transform3D;
    private _keyStates: { [key: number]: boolean } = {};
    @property(Laya.Animator)
    private _animator: Laya.Animator;

    @property(Laya.CharacterController)
    private character: Laya.CharacterController
    onStart(): void {
        this._transform = (this.owner as Laya.Sprite3D).transform;
        this._rigidbody = this.owner.getComponent(Laya.Rigidbody3D);

        this._animator = this.owner.getComponent(Laya.Animator);

        Laya.stage.on(Laya.Event.KEY_DOWN, this, this._onKeyDown);
        Laya.stage.on(Laya.Event.KEY_UP, this, this._onKeyUp);

        this.character = this.owner.getComponent(Laya.CharacterController) as Laya.CharacterController;
    }

    private _onKeyDown(e: Laya.Event): void {
        this._keyStates[e.keyCode] = true;
    }

    private _onKeyUp(e: Laya.Event): void {
        this._keyStates[e.keyCode] = false;
    }

    private deltaTime: number = 0
    onUpdate(): void {
        this.deltaTime = Laya.timer.delta / 1000;

        // 键盘输入检测
        const horizontal = this._keyStates[Laya.Keyboard.A] ? -1 :
            this._keyStates[Laya.Keyboard.D] ? 1 : 0;
        const vertical = this._keyStates[Laya.Keyboard.W] ? 1 :
            this._keyStates[Laya.Keyboard.S] ? -1 : 0;
        // console.log(horizontal,vertical);
        this.apply(horizontal,vertical);
    }

    apply(horizontal: any, vertical: any): void {
        // 计算移动方向（基于当前朝向）
        const moveDirection = new Laya.Vector3();
        this._transform.getForward(moveDirection);
        moveDirection.y = 0; // Ensure movement is only on the XZ plane
        Laya.Vector3.normalize(moveDirection, moveDirection);
        Laya.Vector3.scale(moveDirection, -vertical * this.moveSpeed * this.deltaTime, moveDirection);

        // 计算旋转量
        const rotation = this._transform.localRotationEuler;
        rotation.y -= horizontal * this.rotateSpeed * this.deltaTime;

        // 应用运动
        this.applyMove(moveDirection, rotation);

        // 播放动画
        if (vertical !== 0 || horizontal !== 0) {
            this._animator.play("Run_loop");
        } else {
            this._animator.play("Idle_loop");
        }
        //jump
        Laya.InputManager.hasKeyDown(69) && this.character.jump()
    }


    applyMove(moveDirection: any, rotation: any): void {
        if (this.character) {
            this.character.move(moveDirection)
            this._transform.localRotationEuler = rotation;
        }
        else if (this._rigidbody) {
            this._rigidbody.linearVelocity = moveDirection;
            this._transform.localRotationEuler = rotation;
        } else {
            this._transform.translate(moveDirection, false);
            this._transform.localRotationEuler = rotation;
        }
    }
}
